package com.powernode.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.powernode.domain.ProdComm;
import com.powernode.model.Result;
import com.powernode.service.ProdCommService;
import com.powernode.vo.ProdCommData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @create: 2024/7/18
 * @author: gyhe10
 * @description: 商品评论管理控制层
 **/

@Api(tags = "商品评论接口管理")
@RestController
@RequestMapping("/prod/prodComm")
public class ProdCommController {

    @Resource
    private ProdCommService prodCommService;

    /**
     * 多条件分页查询评论列表
     * @param current 页码
     * @param size 每页显示记录数
     * @param prodName 商品名称
     * @param status 评论状态
     * @return
     */
    @ApiOperation("多条件分页查询评论列表")
    @GetMapping("page")
    @PreAuthorize("hasAuthority('prod:prodComm:page')")
    public Result<Page<ProdComm>> loadProdCommPage(@RequestParam Long current,
                                                   @RequestParam Long size,
                                                   @RequestParam(required = false) String prodName,
                                                   @RequestParam(required = false) Integer status) {
        Page<ProdComm> page = new Page<>(current, size);
        page = prodCommService.page(page, new LambdaQueryWrapper<ProdComm>()
                .eq(ObjectUtil.isNotNull(status), ProdComm::getStatus, status)
                .like(StringUtils.hasText(prodName), ProdComm::getProdName, prodName)
                .orderByDesc(ProdComm::getCreateTime));
        return Result.success(page);
    }

    /**
     * 查看评论详情
     * @param prodCommId 评论ID
     * @return
     */
    @ApiOperation("查看评论详情")
    @GetMapping("{prodCommId}")
    @PreAuthorize("hasAuthority('prod:prodComm:info')")
    public Result<ProdComm> loadProdCommInfo(@PathVariable Long prodCommId) {
        ProdComm prodComm = prodCommService.getById(prodCommId);
        return Result.success(prodComm);
    }

    /**
     * 审核或回复评论
     * @param prodComm 评论对象
     * @return
     */
    @ApiOperation("审核或回复评论")
    @PutMapping
    @PreAuthorize("hasAuthority('prod:prodComm:update')")
    public Result<String> replyOrCheckProdComm(@RequestBody ProdComm prodComm) {
        Boolean modified = prodCommService.replyOrCheckProdComm(prodComm);
        return Result.handle(modified);
    }


    /////////////////// 微信小程序接口 ///////////////////


    /**
     * 小程序查询商品评论总览信息
     * @param prodId 商品ID
     * @return
     */
    @ApiOperation("小程序查询商品评论总览信息")
    @GetMapping("prodComm/prodCommData")
    public Result<ProdCommData> loadWxProdCommData(@RequestParam Long prodId) {
        ProdCommData prodCommData = prodCommService.queryWxProdCommData(prodId);
        return Result.success(prodCommData);
    }

    /**
     * 小程序分页查询单个商品的评论列表
     * @param current 页码
     * @param size 每页显示记录数
     * @param prodId 商品id
     * @param evaluate 评论类型
     * @return
     */
    @ApiOperation("小程序分页查询单个商品的评论列表")
    @GetMapping("prodComm/prodCommPageByProd")
    public Result<Page<ProdComm>> loadWxProdCommPageByProdId(@RequestParam Long current,
                                                             @RequestParam Long size,
                                                             @RequestParam Long prodId,
                                                             @RequestParam Integer evaluate) {
        // 根据商品ID分页查询商品评论列表
        Page<ProdComm> page = prodCommService.queryWxProdCommPageByProdId(current, size, prodId, evaluate);
        return Result.success(page);
    }
}
